package com.fhxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fhxy.model.domain.User;
import com.fhxy.model.domain.request.UserLoginRequest;
import com.fhxy.model.domain.request.UserRegisterRequest;
import com.fhxy.model.domain.request.UserUpdateRequest;
import com.fhxy.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.stream.Collectors;

import static com.fhxy.constant.UserConstant.ADMIN_ROLE;
import static com.fhxy.constant.UserConstant.USER_LOGIN_STATE;

/**
 * ClassName: UserController
 * date: 2024/6/18 10:45
 *
 * @author 卢洋
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 用户注册
     * @param userRegisterRequest 收集前端登录请求信息
     * @return
     */
    @PostMapping("/register")
    public long userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest == null){
            return -1;
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if(StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){
           return -1;
        }
        return userService.userRegister(userAccount, userPassword, checkPassword);
    }


    /**
     * 用户登录
     * @param userLoginRequest 收集前端登录请求信息
     * @param request
     * @return
     */
    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (userLoginRequest == null){
            return null;
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if(StringUtils.isAnyBlank(userAccount,userPassword)){
            return null;
        }

        return userService.userLogin(userAccount, userPassword, request);
    }


    /**
     * 获取当前用户信息
     * 普通用户登入后看见的个人中心
     * @param request
     * @return
     */
    @GetMapping("/current")
    public User getCurrentUser(HttpServletRequest request){
//        没有session就不能显示页面
        Object useObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User currentUser = (User) useObj;
        if (currentUser == null){
            return null;
        }
        Long userId = currentUser.getUserId();
        User user = userService.getById(userId);
        User safetyUser = userService.getSafetyUser(user);
        return safetyUser;
    }


    /**
     * 查询
     * @param userAccount 用户名字
     * @return
     */
    @GetMapping("/search")
    public List<User> searchUsers(String userAccount, HttpServletRequest request){
//        仅管理员可查询
        if (!isAdmin(request)){
            return null;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(userAccount)){
            queryWrapper.like("user_account", userAccount);
        }
        List<User> userList = userService.list(queryWrapper);
        List<User> list = userList.stream().map(user -> {
            return userService.getSafetyUser(user);
        }).collect(Collectors.toList());
        return list;
    }


    /**
     * 用户更改
     *
     * @param userUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public boolean updateUser(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest request) {
        if (userUpdateRequest == null) {
           return false;
        }
        User user = new User();
        BeanUtils.copyProperties(userUpdateRequest, user);
        return userService.updateUser(user);
    }


    /**
     * 注销

     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Integer userLogout(HttpServletRequest request){
        if (request == null){
            return null;
        }
        return userService.userLogout(request);
    }


    /**
     * 删除用户
     * 逻辑删除其实就是更改用户isDelete的值
     * @param id 用户id
     * @return
     */
    @PostMapping("/delete")
    public boolean deleteUser(@RequestBody long id,HttpServletRequest request){
        //        仅管理员可删除
        if (!isAdmin(request)){
            return false;
        }
//        id大于0
        if (id <= 0){
            return false;
        }
        return userService.removeById(id);
    }






    /**
     * 是否为管理员
     * @param request
     * @return
     */
    private boolean isAdmin(HttpServletRequest request){
        //        仅管理员可查询
//         private final Map<String, Object> attributes; 键值对
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
        if (user == null || user.getUserRole() != ADMIN_ROLE){
            return false;
        }
        return true;
    }


}
